{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame 常用的属性\n",
    "- values\t返回 ndarray 类型的对象\n",
    "- index\t    获取索引值\n",
    "- columns\t获取头部\n",
    "- axes\t获取行及列索引\n",
    "- ndim\t维度\n",
    "- shape\t（行，列）\n",
    "- size\t行 * 列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   bookname author price\n",
      "0  python入门   Eric  49.9\n",
      "1  python编程     张健  36.5\n",
      "2  python实战     刘辉  67.4\n"
     ]
    }
   ],
   "source": [
    "# 引入pandas\n",
    "import pandas as pd\n",
    "# 定义一个dataframe数据结构的对象 纵向构建\n",
    "data={'bookname':['python入门','python编程','python实战'],\n",
    "          'author':['Eric','张健','刘辉'],\n",
    "          'price':['49.9','36.5','67.4']}\n",
    "frame_obj=pd.DataFrame(data)\n",
    "print(frame_obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    月考1  月考2  月考3  月考4\n",
      "语文   96   92   83   94\n",
      "数学   85   86   77   88\n",
      "英语   69   90   91   82\n"
     ]
    }
   ],
   "source": [
    "# 构造数据集  横向构建\n",
    "df_data=pd.DataFrame([[96,92,83,94],[85,86,77,88],[69,90,91,82]],\n",
    "                 index=['语文','数学','英语'],\n",
    "                 columns=['月考1','月考2','月考3','月考4'])\n",
    "print(df_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['python入门' 'Eric' '49.9']\n",
      " ['python编程' '张健' '36.5']\n",
      " ['python实战' '刘辉' '67.4']]\n"
     ]
    }
   ],
   "source": [
    "# values 属性\n",
    "print(frame_obj.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2]\n"
     ]
    }
   ],
   "source": [
    "# index 属性\n",
    "print(frame_obj.index.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bookname' 'author' 'price']\n"
     ]
    }
   ],
   "source": [
    "# columns 属性\n",
    "print(frame_obj.columns.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[RangeIndex(start=0, stop=3, step=1), Index(['bookname', 'author', 'price'], dtype='object')]\n"
     ]
    }
   ],
   "source": [
    "# axes 属性\n",
    "print(frame_obj.axes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 3)\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# ndim 属性\n",
    "print(frame_obj.ndim)\n",
    "\n",
    "print(frame_obj.shape)\n",
    "# size 属性\n",
    "print(frame_obj.size)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame 常用的操作方法\n",
    "- DataFrame()\t创建一个DataFrame数据结构的对象\n",
    "- head()\t用于查看数据集的前n行\n",
    "- info()\t快速查看数据的描述\n",
    "- tail()\t用于查看数据集的后n行\n",
    "- T\t转置函数\n",
    "- sum()\t求和函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    A   B   C   D\n",
      "Q   1   2   3   4\n",
      "W   5   6   7   8\n",
      "E   9  10  11  12\n",
      "R  13  14  15  16\n"
     ]
    }
   ],
   "source": [
    "# 1.通过传入数据，行索引，列索引进行创建\n",
    "df_sample=pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],\n",
    "                 index=list('QWER'),columns=list('ABCD'))\n",
    "print(df_sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A   B   C   D\n",
      "Q  1   2   3   4\n",
      "W  5   6   7   8\n",
      "E  9  10  11  12\n"
     ]
    }
   ],
   "source": [
    "print(df_sample.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    A   B   C   D\n",
      "W   5   6   7   8\n",
      "E   9  10  11  12\n",
      "R  13  14  15  16\n"
     ]
    }
   ],
   "source": [
    "print(df_sample.tail(3))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d6ee8951ea1f1e54d5c6db37d467cbab5e82ba9505f1ada5124c3f7e97e9c192"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
